package com.at.bigdata.spark.core.wc

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 * @author cdhuangchao3
 * @date 2023/3/6 8:40 PM
 */
object Spark03_WordCount {

  def main(args: Array[String]): Unit = {
    // TODO 建立和spark框架的连接
    val sparConf = new SparkConf()
      .setMaster("local")
      .setAppName("WordCount")
    val sc = new SparkContext(sparConf)
    val lines: RDD[String] = sc.textFile("datas")
    val words = lines.flatMap(_.split(" "))
    val word21 = words.map(
      word => (word, 1)
    )
    val word2Count = word21.reduceByKey(_ + _)

    // 5.将转换结果采集到控制台打印
    val array = word2Count.collect()
    array.foreach(println)
    // TODO 关闭连接
    sc.stop()
  }
}
